<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

class KBClientAction_register extends KBClientAction_common
{

	function &execute($controller, $manager) {
		
		// check if registration is not required
		if(!$manager->use_registration) {
			$controller->go();
		}	
		
		// check if registered
		$allow_register = $manager->getSetting('register_policy');
		if($manager->is_registered ||  !$allow_register) { 
			$controller->go();
		}
		$register_confirmation = $manager->getSetting('register_confirmation');
		$register_approval = $manager->getSetting('register_approval');

		$view = &$controller->getView('register');
		$view->update = false;
		
		$manager_1 = new MemberModel;
		
		if(isset($this->rp->submit)) {
			
			$errors = $view->validate($this->rp->vars, $manager, $manager_1);
			
			if($errors) {
				$this->rp->stripVars(true);
				$view->setErrors($errors);
				$view->setFormData($this->rp->vars);
			
			} else {
				
				$this->rp->stripVars('stripslashes');

                if($setting['register_confirmation']) {
                    $sent = $manager->sendConfirmRegistration($this->rp->vars, $view);
                } else {
                    $sent = true;
                }
                
				if($sent) {
				
					$this->rp->stripVars('addslashes');
					$view->obj->set($this->rp->vars);
				
					$view->obj->set('id', NULL);
                    $tmsg = 'confirmation_sent';

                    if($register_confirmation) {
                        $view->obj->set('active', 0);
                    } elseif($register_approval) {
                        $view->obj->set('active', 0);
                        $tmsg = 'registration_confirmed_approve';
                    } else {
                        $view->obj->set('active', 1);
                        $tmsg = 'registration_confirmed';
                   }
					$user_id = $manager_1->save($view->obj);
                    $apriv = array('user_id' => $user_id, 'priv_name_id'=> 6, 'grantor' => 0, 'timestamp'=>'NOW()');
                    $auth = new AuthPriv();
                    $sql = ModifySql::getSql('INSERT', $auth->tbl->priv, $apriv);
                    $manager_1->db->Execute($sql) or die(db_error($sql));
                    
					$controller->go('success_go', false, false, $tmsg);
				
				} else {
					$this->rp->stripVars(true);
					$view->setFormData($this->rp->vars);
					$view->msg_id = 'confirmation_not_sent';
				}
			}
		}
		
		return $view;
	}
	
}
?>